<template>
  <div>
    <van-dialog
      v-model="validImgShow"
      :title="$t('操作验证')"
      show-cancel-button
      @confirm="submitValidImgFunc"
      @cancel="cancelValidImgFunc"
      :before-close="handleBeforeClose"
      confirm-button-color="#006AFF"
    >
      <p class="valid-img" v-if="validImgSrc"><img :src="validImgSrc" /></p>
      <div class="valid-img-dialog">
        <van-field
          size="small"
          type="primary"
          v-model="imgValidForm.validImgInput"
          style="width: 160px; margin-right: 10px"
          :placeholder="$t('请输入图片验证码')"
        ></van-field>
        <van-button
          size="small"
          type="primary"
          @click="setValidImageFunc"
          class="common-btn"
          >{{ $t('看不清？换一张') }}</van-button
        >
      </div>
    </van-dialog>
  </div>
</template>
<script>
import { userService } from '@/services/user'
export default {
  name: 'ValidImgLayer',
  props: {
    validImgShow: {
      type: Boolean,
      default: false,
      required: true,
    },
    phoneNumber: {
      type: [String, Number],
    },
    loginNoPhone: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      showDialog: false,
      validImgSrc: '',
      imgValidForm: {
        validImgInput: '',
      },
      imgRules: {
        validImgInput: [
          {
            required: true,
            message: this.$t('图形验证码不能为空'),
            trigger: 'blur',
          },
        ],
      },
    }
  },
  watch: {
    validImgShow: function (newVal, oldVal) {
      // 刷新图片验证码
      if (newVal) {
        this.setValidImageFunc()
      }
    },
  },
  methods: {
    setValidImageFunc() {
      this.imgValidForm.validImgInput = ''
      if (!this.phoneNumber) {
        this.validImgSrc =
          process.env.VUE_APP_BASE_Z_API +
          '/scm-whale-pc/verify-code/captcha.jpg?token=' +
          new Date().getTime()
      } else {
        this.validImgSrc =
          // process.env.BASE_URL +
          process.env.VUE_APP_BASE_Z_API +
          '/scm-whale-pc/verify-code/image?mobile=' +
          this.phoneNumber +
          '&t=' +
          new Date().getTime()
      }
      console.log('dasdadas', this.validImgSrc)
    },
    cancelValidImgFunc() {
      this.$emit('closeValidImg')
    },
    submitValidImgFunc() {
      if (!this.imgValidForm.validImgInput) {
        this.$toast(this.$t('请输入图片验证码'))
        return
      }
      this.$emit('validComplete', this.imgValidForm.validImgInput)
    },
    handleBeforeClose(action, done) {
      done(false)
    },
  },
}
</script>
<style lang="scss" scoped>
::v-deep .van-dialog__header {
  padding-top: 30px;
}
::v-deep .van-dialog__footer {
  width: 100% !important;
}
.valid-img-dialog {
  display: flex;
  align-items: center;
  padding: 10px 0;
}
.valid-img {
  text-align: center;
}
.common-btn {
  background: #006aff;
  border: none;
}
</style>
